(function($){
    $.showCourseAddForm = function(t) {
        $(t).parent().hide();
        if ($('body').hasClass('effect')) {
            $(t).parent().parent().find('.form-container').show('normal');
        }
        else {
            $(t).parent().parent().find('.form-container').show();
        }
        return false;
    };

    $.hideCourseAddForm = function(t) {
        $(t).parents('form').resetForm();
        $(t).parents('form').find('.ajax-message').html("");
        if ($('body').hasClass('effect')) {
            $(t).parents('form').parent().hide('normal', function() {
                $('.course-add-form-toggle').parent().show();
            });
        }
        else {
            $(t).parents('form').parent().hide(10, function() {
                $('.course-add-form-toggle').parent().show();
            });
        }
    };

    $.inputLangChange = function(t) {
        var value = $(t).find('option:selected').attr('value');
        var url = $(t).parents('form').find('input[name=nice_url]').attr('value');
        var data = {
            action: 'input_lang_change',
            lang : value,
            nice_url : url
        }
        var send = $.param(data);
        $.ajax({
            url : 'ajax/course-ajax.php',
            data : send,
            type : 'post',
            dataType: 'json',
            success: function(data) {
                if (data.code == 1) {
                    $(t).parents('form').find('input[name=course_name]').attr('value', data.name);
                    $(t).parents('form').find('textarea[name=course_description]').val(data.description);
                }
            }
        });
    };

    $.submitCourseAddForm = function(t) {
        $.submitForm(t, function(form, data){
            if (data.code == 1) {
                $('#course-json-admin').pageJsonRefresh('last');
                $(form).resetForm();                
                $(form).find('.ajax-message').htmlTimer(data.message, 5000);
            }
            else {
                $(form).find('.ajax-message').htmlTimer(data.message, 5000);
            }
        });
    };
})(jQuery);

$(function(){
    $("#course-json").pageJson({
        url : "ajax/course-json.php",
        type : "post",
        admin: 0,
        errorMessage: "Có lỗi xảy ra, không thể lấy được dữ liệu",
        contentFormat: 'ul',
        onSuccess : function() {
            $(".document-tree").treeview({
                animated: 'fast'
            });
            $.formDecorate();
        }
    });

    $("#course-json-admin").pageJson({
        url : "ajax/course-json.php",
        type : "post",
        admin: 1,
        errorMessage: "Có lỗi xảy ra, không thể lấy được dữ liệu",
        contentFormat: 'ul',
        onSuccess : function() {
            $(".document-tree").treeview({
                animated: 'fast'
            });
            $.formDecorate();
        },
        formFunctions : [
        {
            selector : '.course-edit-ajax-submit',
            func : function(s) {
                return $.submitForm(s, function(form, data){
                    if (data.code == 1) {
                        $("#course-json-admin").pageJsonRefresh();
                    }
                    else {
                        $(form).find(".ajax-message").htmlTimer(data.message, 5000);
                    }
                });
            }
        },
        {
            selector : '.course-delete-ajax-submit',
            func : function(s) {
                var data = {
                    action : 'delete_course'
                }
                var send = $.param(data);
                $.ajax({
                    url : 'ajax/message-ajax.php',
                    data : send,
                    type : 'POST',
                    dataType : 'html',
                    success : function(message) {
                        var c = confirm(message);
                        if (c) {
                            data = {
                                action : 'delete_course',
                                nice_url : $(s).parents('form').find('input[name=nice_url]').attr('value')
                            }
                            send = $.param(data);
                            $(s).parents('form').find('.ajax-load').show();
                            $(".form-button").addClass("disabled");
                            $(".form-button").attr("disabled","disabled");
                            $.ajax({
                                url : 'ajax/course-ajax.php',
                                data : send,
                                type : 'POST',
                                dataType : 'json',
                                success : function(data) {
                                    $(s).parents('form').find('.ajax-load').hide();
                                    $(".form-button").removeAttr("disabled");
                                    $(".form-button").removeClass("disabled");
                                    if (data.code == 1) {
                                        $("#course-json-admin").pageJsonRefresh();
                                    }
                                    else {
                                        $(s).parents('form').find(".ajax-message").htmlTimer(data.message, 5000);
                                    }
                                },
                                error : function() {
                                    $(s).parents('form').find('.ajax-load').hide();
                                    $(".form-button").removeAttr("disabled");
                                    $(".form-button").removeClass("disabled");
                                }
                            });
                               
                        }
                    }
                });
                return false;
            }
        },
        {
            selector : '.input-lang-changer',
            func : function(s) {
                $.inputLangChange(s);
            }
        }
        ]
    });
   
    $('.course-add-form-toggle').click(function(){
        return $.showCourseAddForm(this);
    });

    $('.course-add-cancel').click(function(){
        $.hideCourseAddForm(this);
    });

    $('.course-add-ajax-submit').click(function(){
        $.submitCourseAddForm(this);
    });
});